//
//  YNBaseViewController.m
//  CarService
//
//  Created by Nimo.Yu on 15/1/29.
//  Copyright (c) 2015年 lexingsoft. All rights reserved.
//

#import "YNBaseViewController.h"

@interface YNBaseViewController ()
@property (strong, nonatomic) NSTimer *loadingTimer;
@property (weak, nonatomic) IBOutlet UIView *loadingView;
@property (weak, nonatomic) IBOutlet UIImageView *loadingImageView;
@property (strong, nonatomic) UILabel * loadingLabel;
@end

@implementation YNBaseViewController

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //    [self.view setBackgroundColor:[ColorManager colorWithViewBackground]];
    [self setupNothingView];
    [self setupLoadingView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc] init];
    self.navigationItem.backBarButtonItem.title=@"";
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    CGSize thisSize=self.view.bounds.size;
    self.loadingView.frame=CGRectMake(0, 0, thisSize.width, thisSize.height);
    CGSize imageSize=CGSizeMake(400/2, 400/2);
    self.loadingImageView.frame=CGRectMake((thisSize.width-imageSize.width)/2, (thisSize.height-imageSize.height)/2-50, imageSize.width, imageSize.height);
    CGSize labelSize=CGSizeMake(70,20);
    self.loadingLabel.frame=CGRectMake((thisSize.width-labelSize.width)/2+5, (thisSize.height-labelSize.height)/2+20, labelSize.width, labelSize.height);
    
    [self.view bringSubviewToFront:self.loadingView];
}

- (void)showLoadingView{
    if (self.loadingView.hidden==NO) {
        return;
    }
    [self.view bringSubviewToFront:self.loadingView];
    self.loadingView.hidden=NO;
    self.loadingTimer=[NSTimer scheduledTimerWithTimeInterval:1/30.0f target:self selector:@selector(updateLoading) userInfo:nil repeats:YES];
}

- (void)hideLoadingView{
    self.loadingView.hidden=YES;
    [self.loadingTimer invalidate];
    self.loadingTimer=nil;
}

- (void)setNothingViewIsHide:(BOOL)isHide
{
    self.yn_nothingLabel.hidden=isHide;
    self.yn_nothingImageView.hidden=isHide;
    if (!isHide) {
        [self.view bringSubviewToFront:self.yn_nothingLabel];
        [self.view bringSubviewToFront:self.yn_nothingImageView];
    }
}

- (void)showTimeOutView
{
}

- (void)hideTimeOutView
{
}

- (void)setTimeOutAction:(SEL)action target:(id)target
{
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupNothingView{
    UIImageView *imageView=[UIImageView new];
    imageView.image=[UIImage imageNamed:@"page_icon_empty"];
    [self.view addSubview:imageView];
    imageView.hidden=YES;
    self.yn_nothingImageView=imageView;
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(imageView.superview);
        make.centerY.equalTo(imageView.superview).offset(-64);
//        make.height.equalTo(@(100));
//        make.width.equalTo(@(100));
    }];
    
    UILabel *label=[UILabel new];
    label.text=@"暂无内容";
    label.font=[UIFont systemFontOfSize:14];
    label.textColor=[UIColor grayColor];
    [self.view addSubview:label];
    label.hidden=YES;
    self.yn_nothingLabel=label;
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView.bottom).offset(20);
        make.centerX.equalTo(label.superview);
    }];
}

- (void)setupLoadingView{
    
    UIView *loadingBgView=[UIView new];
    loadingBgView.backgroundColor=[ColorManager groupTableViewBackgroundColor];
    [self.view addSubview:loadingBgView];
    UIImageView *imageView=[UIImageView new];
    [loadingBgView addSubview:imageView];
    UILabel * label = [UILabel new];
    label.text = @"加载中...";
    label.textColor = [ColorManager colorWithHexString:@"#9A9A9A"];
    [loadingBgView addSubview:label];
    self.loadingLabel=label;
    self.loadingImageView=imageView;
    self.loadingView=loadingBgView;
    self.loadingView.hidden=YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)updateLoading
{
    static int index=0;

    index=index % 50;
    NSString *imageName=[NSString stringWithFormat:@"loading00%02d",index];
    self.loadingImageView.image=[UIImage imageNamed:imageName];
    index++;
}
@end
